package dao

import (
	"goodServer/basic/config"
	"goodServer/basic/model"
)

func GoodList(page, size int64) (good []model.Good, err error) {
	page = page
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	err = config.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&good).Error
	if err != nil {
		return nil, err
	}
	return good, nil
}

func GoodUpdateUrl(id int64, oss string) (good model.Good, err error) {
	err = config.DB.Model(good).Where("id = ?", id).Update("url", oss).Error
	if err != nil {
		return model.Good{}, err
	}
	return good, nil

}

func GoodListOne(name string) (good model.Good, err error) {
	err = config.DB.Where("name =?", name).Find(&good).Error
	if err != nil {
		return model.Good{}, err
	}
	return good, nil
}

func GoodListR() (good []model.Good, err error) {
	err = config.DB.Find(&good).Error
	if err != nil {
		return nil, err
	}
	return good, nil
}
